<?php
class control_bug extends core_action{
	
	function __construct(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
	}
	
	function index(){
		$model = new core_model_bug();
		$tn = $model->tableName();
		$catetn = core_lib_constant::TABLE_PRE."cate";
		$page = intval($_REQUEST['p']) ? intval($_REQUEST['p']) : 1;
		switch ($_REQUEST['c']){
			case 1://未处理
				$condi = "{$tn}.is_sure=0 or {$tn}.is_ignore=0";
				break;
			case 2://已确认
				$condi = "{$tn}.is_sure=1";
				break;
			case 3://已忽略
				$condi = "{$tn}.is_ignore=1";
				break;
			case 4://需要公开
				$ymd=date('Y-m-d',strtotime('-30 day'));
				$condi = "({$tn}.is_sure=1 or {$tn}.is_ignore=1) and {$tn}.is_open=0 and {$tn}.create_ymd<'{$ymd}'";
				break;
			case 5://搜索
				$key = Qutil::filter($_REQUEST['key']);
				$condi = "{$tn}.title like '%{$key}%' or {$tn}.content like '%{$key}%' or {$tn}.username like '%{$key}%'";
				break;
			default:
				$condi = '';
		}
		$model->setPage($page);
		$model->setLimit(core_lib_constant::PAGE_NUM);
		$model->setCount(TRUE);
		$rs = $model->select($condi,"","","order by {$tn}.bid desc",array($catetn=>"{$tn}.cate_id={$catetn}.cid"));
		$params['bug'] = $rs->items;
		$params['pageSize'] = $rs->pageSize;
		$params['page'] = $page;
		$params['key'] = $key;
		$params['bug_cur'] = 'current';
		$_GET['key'] = str_replace('&', '', $key);
		$_GET['c'] = intval($_REQUEST['c']);
		$this->render('simpla/bug_list.php', $params);
	}
	
	function show(){
		$bid = intval($_REQUEST['bid']);
		if($bid <= 0) $this->showMsg("缺少参数",1);
		$srvBug = new core_service_bug($bid);
		if($_POST){
			$post = Qutil::filter($_POST);
			if($post['token'] != md5($_COOKIE['adminKey'])){
				$this->showMsg("非法请求",1);
			}
			if($srvBug->edit($post) === false){
				$this->showMsg("编辑出错",1);
			}
			$msg = "修改";
			if($post['handle'] == 1 && $post['level']>0 && $post['rank']>0 && $post['reason']){
				if($srvBug->bugSure($post['level'], $post['rank'], $post['reason']) === false){
					$this->showMsg("确认出错".$srvBug->getError(),1);
				}
				$msg = "确认";
			}
			if($post['handle'] == 2 && $post['reason']){
				if($srvBug->bugIgnore($post['reason']) === false){
					$this->showMsg("忽略出错".$srvBug->getError(),1);
				}
				$msg = "忽略";
			}
			if($post['handle'] == 3){
				if($srvBug->bugOpen() === false){
					$this->showMsg("公开出错".$srvBug->getError(),1);
				}
				$msg = "公开";
			}
			$model = new core_model_adminlog();
			$model->addLog(2, "对“{$srvBug->get('title')}”(编号{$bid})的漏洞进行了{$msg}操作");
			$this->showMsg($msg.'成功',2);
		}
		$params['token'] = md5($_COOKIE['adminKey']);
		$params['bug'] = $srvBug->get();
		$params['bug_cur'] = 'current';
		$this->render('simpla/bug.php', $params);
	}
}